package com.core.cloudbusiness.system.debug.controller;

import com.core.cloudcommon.CommonResult;
import com.core.cloudcommon.trace.TraceLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author =====Lqj=====
 * @className LoginDebugController
 * @description 登录调试控制器，用于排查登录500错误
 * @create 2024/12/17 10:30
 **/
@Slf4j
@RestController
@RequestMapping("/api/debug")
public class LoginDebugController {

    /**
     * 测试基本的控制器响应
     */
    @GetMapping("/test")
    public CommonResult<Map<String, Object>> test() {
        log.info("调试控制器测试接口被调用");
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "调试控制器正常工作");
        result.put("timestamp", System.currentTimeMillis());
        
        return CommonResult.OK(result);
    }

    /**
     * 测试异常处理
     */
    @GetMapping("/test-exception")
    public CommonResult<Map<String, Object>> testException() {
        log.info("测试异常处理");
        
        // 故意抛出异常来测试全局异常处理器
        throw new RuntimeException("这是一个测试异常");
    }

    /**
     * 测试CommonResult的各种返回格式
     */
    @GetMapping("/test-result")
    public CommonResult<Map<String, Object>> testResult() {
        log.info("测试CommonResult返回格式");
        
        Map<String, Object> data = new HashMap<>();
        data.put("test", "success");
        data.put("code", 200);
        
        return CommonResult.OK(data);
    }

    /**
     * 模拟登录成功的返回格式
     */
    @PostMapping("/mock-login")
    public CommonResult<Map<String, Object>> mockLogin(@RequestBody Map<String, Object> request) {
        log.info("模拟登录请求: {}", request);
        
        try {
            // 模拟登录成功的数据结构
            Map<String, Object> loginResult = new HashMap<>();
            loginResult.put("id", 1001L);
            loginResult.put("token", "mock_jwt_token_12345");
            loginResult.put("tokenTime", 24);
            loginResult.put("loginTime", System.currentTimeMillis());
            
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("id", 1001L);
            userInfo.put("userName", "test_admin");
            userInfo.put("realName", "测试管理员");
            userInfo.put("phone", "13800001001");
            userInfo.put("email", "test_admin@example.com");
            userInfo.put("departName", "技术部");
            
            loginResult.put("cloudSystemUserDTO", userInfo);
            
            log.info("模拟登录成功，返回数据: {}", loginResult);
            return CommonResult.OK(loginResult);
            
        } catch (Exception e) {
            log.error("模拟登录过程中出现异常", e);
            throw e;
        }
    }

    /**
     * 测试参数验证
     */
    @PostMapping("/test-validation")
    public CommonResult<Map<String, Object>> testValidation(@RequestBody Map<String, Object> request) {
        log.info("测试参数验证: {}", request);
        
        // 检查必要参数
        if (!request.containsKey("phone")) {
            throw new IllegalArgumentException("手机号不能为空");
        }
        
        if (!request.containsKey("smsCode")) {
            throw new IllegalArgumentException("验证码不能为空");
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "参数验证通过");
        result.put("phone", request.get("phone"));
        result.put("smsCode", request.get("smsCode"));
        
        return CommonResult.OK(result);
    }

    /**
     * 测试链路追踪
     */
    @GetMapping("/test-trace")
    public CommonResult<Map<String, Object>> testTrace() {
        log.info("测试链路追踪功能");
        TraceLogger.info("这是一条链路追踪日志");
        TraceLogger.business("调试测试", "测试链路追踪功能", "成功");
        
        Map<String, Object> result = new HashMap<>();
        result.put("message", "链路追踪测试完成");
        result.put("traceId", "请查看日志中的TraceId");
        
        return CommonResult.OK(result);
    }
}
